<?php
require 'cofd/common.php';
$mn = $_GET['postid'];
echo "<script>sessionStorage.setItem('postnow', '" . $mn . "');</script>";
$sql = "SELECT phead, pbody,zt,fromer FROM post WHERE id = $mn";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
    $title = $row['phead'];
    $content = $row['pbody'];
    if($row['zt'] == '0'){
        die("帖子审核未通过");
    }
}else{
die("此帖子不存在或已被删除");
}
$poster = $row['fromer'];
$sql2 = "SELECT tximg,uname FROM user WHERE id = $poster ";
$result2 = $conn->query($sql2);
$row2 = $result2->fetch_assoc();
function getComments($mn, $conn) {
    $sql = "SELECT c.comment, c.created_at, u.tximg, u.uname FROM comments c JOIN user u ON c.user_id = u.id WHERE c.post_id = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $mn);
    $stmt->execute();
    $result = $stmt->get_result();
    return $result;
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>帖子详情</title>
    <link rel="stylesheet" href="ast/post-in.css">
    <link rel='stylesheet' href='ast/message/message.min.css'>
    <script src='ast/message/message.min.js'></script>
</head>
<body>

    <div class="post-container">
    <a href="javascript:void(0);" onclick="img_message();">  
    <img src="<?php echo $row2['tximg']; ?>" class="avatar">  
</a>
        <div class="xhxwn">作者：<?php
        echo $row2['uname'];
        ?>
        </div>
        <div class="post-title"><?php echo $title;?></div>
        <div class="post-content">
            <?php echo $content; ?>
        </div>
    </div>
    <input type="hidden" id="hiddenInput" value=<?php echo $poster; ?>>
    <div class="comments-container">
        <div class="comments-title">评论区</div>
        <div id="comments-list">
            <?php
            $result_comments = getComments($_GET['postid'], $conn);
            if ($result_comments->num_rows > 0) {
                while($row_comment = $result_comments->fetch_assoc()) {
                    echo '<div class="comment">';
        echo '<div class="comment-header">';
        echo '<img src="' . htmlspecialchars($row_comment['tximg']) . '" class="comment-avatar" alt="用户头像">';
        echo '<span class="comment-author">' . htmlspecialchars($row_comment['uname']) . '</span>';
        echo '</div>';
        echo '<div class="comment-body">';
        echo '<div class="comment-text">' . nl2br(htmlspecialchars($row_comment['comment'])) . '</div>';
        echo '<div class="comment-date">' . htmlspecialchars($row_comment['created_at']) . '</div>';
        echo '</div>';
        echo '</div>';
                }
            } else {
                echo '<p>一个评论也没有哦</p>';
            }
            $conn->close();
            ?>
        </div>
        <div class="comment-form">
            <textarea id="comment-input" placeholder="善语结善缘，恶语伤人心"></textarea>
            <button onclick="postconment()">提交评论</button>
        </div>
    </div>

</body>
</html>
<script src="ast/comment.js"></script>